package database

import (
	"gintest/models"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var gdb *gorm.DB

func Setup() {
	var err error
	dsn := "root:123456@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local"
	gdb, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal("failed to connect database:", err)
	}

	// 传入你的模型，比如 User
	gdb.AutoMigrate(&models.Product{})
}

func GetDB() *gorm.DB {
	if gdb == nil {
		log.Fatal("database not initialized")
	}
	return gdb
}
